// pages/pay/paidOrder/paidOrder.js
const handler=require("../../../utils/function.js")
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        img: handler.userImg,
        order: null,
        driver: wx.getStorageSync("serverDriver"),
        reduce: null,
        total: null,
        checkOrder: true,
        checkCoupon: true,
        wechatOrZhifubao: true,
        order_no: '',
      couponList: [],
      choose_coupon:{
        is_open:false,
        message:{}
      },
    },
    // checkOrderHandler 查看详情事件
    checkOrderHandler: function() {
        this.setData({
            checkOrder: !this.data.checkOrder
        });
    },
    // checkCouponHandler 查看优惠券事件
    checkCouponHandler: function() {
        this.setData({
            checkCoupon: !this.data.checkCoupon
        });
    },
    // callPhoneHandler 打电话事件
    callPhoneHandler: function(e) {
        console.log(e.currentTarget.dataset.phone)
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.phone
        })
    },
    //
    bindCasPickerChange: function() {},
    // choosePayWayHandler 支付方式事件
    choosePayWayHandler: function() {
        this.setData({
            wechatOrZhifubao: !this.data.wechatOrZhifubao
        });
    },
    // payHandler 支付事件
    payHandler: function(e) {
        console.log('支付方式选择', e)
        var wxpay = e.currentTarget.dataset.wxpay;
        var order_no = e.currentTarget.dataset.order_no;
        // var pay_price=this.data.pay_price;
      console.log(wxpay,'支付现金')
        const that = this;
        wx.showModal({
            title: '确认支付',
            content: "实付现在支付",
            success: function(res) {
                if (res.confirm) {
                    const that = this;
                    app.request({
                        url: api.order.prepay,
                        method: 'post',
                        data: {
                            order_no: order_no,
                            pay: wxpay,
                            pay_price: 0.01,
                        },
                        success: function(rs) {
                            if (rs.code == 1) {}
                            wx.requestPayment({
                                'timeStamp': rs.data.timeStamp,
                                'nonceStr': rs.data.nonceStr,
                                'package': rs.data.package,
                                'signType': rs.data.signType,
                                'paySign': rs.data.paySign,
                                'success': function(res) {
                                    console.log('支付回调结果', res)
                                    wx.showToast({
                                        title: '支付成功',
                                        icon: "success"
                                    });
                                  wx.redirectTo({
                                      url: "/pages/userOrder/allOrder/allOrder"
                                    });
                                },
                                'fail': function(res) {
                                    console.log('支付回调结果1', res)
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: "none"
                                    });
                                },
                                'complete': function(res) {
                                    console.log('支付回调结果2', res)
                                }
                            })
                        }
                    })
                    /* wx.showToast({
                         title: '支付成功',
                         icon: "success"
                     });
                     wx.redirectTo({
                         url: "../paidDetail/paidDetail?pay=" + wxpay + '&order_no=' + order_no
                     });*/
                }
            },
            fail: function() {
                wx.showToast({
                    title: '支付失败',
                    icon: "none"
                })
            }
        })
    },
  open_coupon:function(){
    const { choose_coupon}=this.data;
    choose_coupon.is_open=true;
    this.setData({ choose_coupon})
  },
    // 选择优惠券
  getCoupon:function(e){
    var id=e.detail.value;
    if(id!=0){
      const { choose_coupon, couponList, pay_price, order } = this.data;
      choose_coupon.message = couponList[id];
      console.log(pay_price, choose_coupon, id, '2222222222')
      var paid = parseFloat(order.actual_price) - parseFloat(choose_coupon.message.price);
      console.log(paid)
      this.setData({ choose_coupon, pay_price: paid })
    }
  },
  //关闭优惠券
  closeCouponHandler:function(){
    const { choose_coupon } = this.data;
    choose_coupon.is_open = false;
    choose_coupon.message = {};
    this.setData({ choose_coupon })
    
  },
  //选择优惠券
  getCouponHandler:function(){
    const { choose_coupon } = this.data;
    choose_coupon.is_open = false;
    this.setData({ choose_coupon })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        app.request({
            url: api.order.order_cost,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
              console.log(rs,'2222222222222')
                if (rs.code == 1) {
                    if (rs.data.is_pay == 1) {
                        wx.redirectTo({
                            url: '../paidDetail/paidDetail?order_no=' + that.data.order_no,
                        })
                    }
                    that.setData({
                      order: rs.data,
                      pay_price: rs.data.actual_price,
                      work_phone: rs.data.work_phone,
                      signature: rs.data.signature
                    })
                  // const couponList=[{name:"优惠券名称",price:'价格'}];
                  // for (var i = 0; i < rs.data.CouponList.length;i++){
                  //   couponList.push(rs.data.CouponList[i]);
                  // }
                  // that.setData({ couponList})
                  //  
                  // console.log(rs.data.CouponList, couponList,'rs.data.CouponList')
                }
            }
        })
    },
    priceRule:function(e){
      console.log(e,'2222222222');
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    },
})